package com.logic.modular.game.core.service.base;

import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logic.modular.game.core.mapper.PvpMatchEndMapper;
import com.logic.modular.game.core.domain.PvpMatchEnd;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 基础服务-pvp对局信息
 *
 * @author cxy
 * @since 2024-09-12 17:17:22
 */
@Slf4j
@Service
public class PvpMatchEndBaseServiceImpl extends ServiceImpl<PvpMatchEndMapper, PvpMatchEnd> {
    private static final String cacheKey = "pvpMatch:%d:endInfo";
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    private ValueOperations<Object, PvpMatchEnd> valueOperations;
    @Resource
    private HashOperations<Object, Object, PvpMatchEnd> hashOperations;
    @Resource
    private PvpMatchEndMapper pvpMatchEndMapper;
    
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  基础查询
    public PvpMatchEnd select(
            @NotNull(message = "keyValue1不能为空") Long matchId,
            @NotNull(message = "keyValue2不能为空") Long playerId
    ){
        PvpMatchEnd pvpMatchEnd = selectCache(matchId, playerId);
        if (pvpMatchEnd == null){
            pvpMatchEnd = super.getById(playerId);
        }
        return pvpMatchEnd;
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////    转换
    public Map<String, PvpMatchEnd> listToMap(List<PvpMatchEnd> list) {
        return list.stream().collect(Collectors.toMap(
                k -> k.getPlayerId().toString(),
                v -> v
        ));
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  更新插入
    @Override
    public boolean updateById(PvpMatchEnd entity) {
        boolean b = super.updateById(entity);
        PvpMatchEnd byId = super.getById(entity.getId());
        savaCache(byId);
        return b;
    }

    @Override
    public boolean save(PvpMatchEnd entity) {
        boolean save = super.save(entity);
        entity = super.getById(entity.getId());
        savaCache(entity);
        return save;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  缓存部分
    public void savaCache(Long keyValue1, Map<String, PvpMatchEnd> map) {
        String key1 = String.format(cacheKey, keyValue1);
        hashOperations.putAll(key1, map);
        redisTemplate.expire(key1, 60, TimeUnit.MINUTES);
    }
    public void savaCache(PvpMatchEnd entity) {
        String key1 = String.format(cacheKey, entity.getMatchId());
        String key2 = entity.getPlayerId().toString();
        hashOperations.put(key1, key2, entity);
        redisTemplate.expire(key1, 30, TimeUnit.DAYS);
    }
    public PvpMatchEnd selectCache(
            @NotNull(message = "keyValue1不能为空") Long matchId,
            @NotNull(message = "keyValue2不能为空") Long playerId
    ) {
        String key1 = String.format(cacheKey, matchId);
        String key2 = playerId.toString();
        redisTemplate.expire(key1, 60, TimeUnit.MINUTES);
        return hashOperations.get(key1, key2);
    }
    public List<PvpMatchEnd> listCache(
            @NotNull(message = "keyValue1不能为空") Long keyValue1
    ) {
        String key1 = String.format(cacheKey, keyValue1);
        redisTemplate.expire(key1, 60, TimeUnit.MINUTES);
        return new ArrayList<>(hashOperations.entries(key1).values());
    }   
    public void removeCache(PvpMatchEnd entity) {
        String key1 = String.format(cacheKey, entity.getMatchId());
        String key2 = entity.getId().toString();
        hashOperations.delete(key1, key2);
    } 
}
